package AddressService

import (
	"QingDai-Gin/api/models"
	"QingDai-Gin/config"
	"gorm.io/gorm"
)

func GetAllAddress() ([]models.Address, *gorm.DB) {
	var addresses []models.Address
	Mysql := config.Mysql()
	tx := Mysql.Table("address__address").Find(&addresses)
	return addresses, tx
}

func GetAddressById(id int) (models.Address, *gorm.DB) {
	var address models.Address
	Mysql := config.Mysql()
	tx := Mysql.Table("address__address").First(&address, id)
	return address, tx
}

func GetAddressesByP(p map[string]any) ([]models.Address, *gorm.DB) {
	var addresses []models.Address
	Mysql := config.Mysql()
	tx := Mysql.Table("address__address")
	for key, value := range p {
		tx.Where(key+" = ?", value)
	}
	tx.Find(&addresses)
	return addresses, tx
}
func GetAddressByP(p map[string]any) (models.Address, *gorm.DB) {
	var address models.Address
	Mysql := config.Mysql()
	tx := Mysql.Table("address__address")
	for key, value := range p {
		tx.Where(key+" = ?", value)
	}
	tx.First(&address)
	return address, tx
}

func CreateAddress(address models.Address) {
	Mysql := config.Mysql()
	Mysql.Table("address__address").Create(address)
}

func UpdateAddress(address models.Address) {
	Mysql := config.Mysql()
	Mysql.Table("address__address").Save(address)
}

func DeleteAddress(address models.Address) {
	Mysql := config.Mysql()
	Mysql.Table("address__address").Delete(address)
}
